<%@ page language="java" import="com.alibaba.fastjson.JSONArray" pageEncoding="UTF-8" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="okhttp3.*" %>
<%@ page import="org.apache.axis.encoding.Base64" %>
<%@ page import="weaver.common.DateUtil" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="weaver.general.BaseBean" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="java.text.ParseException" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.util.*" %>
<%@ page import="java.util.concurrent.TimeUnit" %>
<%--流程查询库存量数据调用SAP查询库存接口--%>
<%
    JSONObject jsonData = new JSONObject();
    RecordSet rs = new RecordSet();
    try {
        JSONArray datasList = new JSONArray();
        String tablename = "formtable_main_2176";
        String requestid = "3972000";
        JSONObject jsonMap = new JSONObject();
        rs.executeQuery("select * from " + tablename + " where requestid=?", requestid);
        rs.next();
        String[] columnName = rs.getColumnName();
        JSONObject mainMap = new JSONObject();
        for (String column : columnName) {
            String columnValue = Util.null2String(rs.getString(column));
            mainMap.put(column.toLowerCase(), columnValue);
        }
        out.println(">>>>>mainMap="+mainMap+"<br/>");
        //唯一标识号
        //String wybsh = mainMap.get("wybsh");
        String wybsh=String.format("%012d", requestid);
        rs.executeQuery("select sapxtdz,sapxtdzzh,sapxtdzmm from uf_HR_xtdz where sfqy='0'");
        //地址
        String sapxtdz="";
        //账号
        String sapxtdzzh="";
        //密码
        String sapxtdzmm="";
        while(rs.next()) {
            sapxtdz = rs.getString("sapxtdz");
            sapxtdzzh = rs.getString("sapxtdzzh");
            sapxtdzmm = rs.getString("sapxtdzmm");
        }

        rs.executeQuery("select loginid from hrmresource where id='" + mainMap.get("xqrxm") + "'");
        //工号
        String loginid = "10000100";
//        if (rs.next()) {
//            loginid = rs.getString("loginid");
//        }

        jsonMap.put("PERNR", loginid);
        jsonMap.put("INFTY", "0016");
        jsonMap.put("BEGDA", mainMap.getString("xhtksrq1").replaceAll("-", ""));
        jsonMap.put("ENDDA", "99991231");
        jsonMap.put("CTTYP", mainMap.getString("xhtlx1"));
        jsonMap.put("ZZCTEDT", mainMap.getString("xhtzzrq1").replaceAll("-", ""));
        jsonMap.put("CTNUM", mainMap.getString("hth1"));
        jsonMap.put("ZZHTQXFL", mainMap.getString("xhtqxfl1"));
        jsonMap.put("ZZHTQX", mainMap.getString("htqxn1"));
        jsonMap.put("ZZSYQX", "");
        jsonMap.put("ZZHTGWLX", mainMap.getString("htgwlx1"));
        jsonMap.put("ZZHTZT", mainMap.getString("xhtzt1"));
        //210924增加甲方单位
        String jfdw = "";
        rs.executeQuery("select field3 from CUS_FIELDDATA where scopeid=-1 and id='"+mainMap.get("xqrxm")+"'");
        if(rs.next()){
            jfdw=rs.getString("field3");
        }
        jsonMap.put("ZZJFDW", jfdw);

        //新增P0019
        //接口明细一
        //判断见习截止日期不为空
        String lastDay = "";
        if (!mainMap.get("xhtzzrq1").equals("")) {
            //子表纳期-7  的日期时间
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar calendar = Calendar.getInstance();
            Date date1 = null;
            try {
                date1 = sdf.parse(mainMap.getString("xhtzzrq1"));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            calendar.setTime(date1);
            int day = calendar.get(Calendar.DATE);
            // 后一天为 +1   前一天 为-1
            calendar.set(Calendar.DATE, day - 30);
            lastDay = sdf.format(calendar.getTime());
        }

        List dataList4 = new ArrayList();
        Map dataMap4 = new HashMap();
        dataMap4.put("BEGDA", mainMap.getString("xhtksrq1").replaceAll("-", ""));
        dataMap4.put("ENDDA", "99991231");
        dataMap4.put("TMART", "Z1");
        dataMap4.put("TERMN", mainMap.getString("xhtzzrq1").replaceAll("-", ""));
        dataMap4.put("MNDAT", lastDay.replaceAll("-", ""));
        dataList4.add(dataMap4);
        jsonMap.put("P0019", dataList4);
        datasList.add(jsonMap);
        out.println(">>>>>datasList="+datasList+"<br/>");
        JSONObject resultJson = handleOKHttpRequest("EC122", datasList);
        jsonData.put("resultJson", resultJson);
    } catch (Exception e) {
        e.printStackTrace();
    }
    out.print(jsonData.toJSONString());
%>
<%!
    private static BaseBean baseBean = new BaseBean();
    private static final String URL = "http://10.48.44.179:50000/RESTAdapter/ecthcm/interface";
    private static final String USERNAME = "po_user";
    private static final String PASSWORD = "Init123456!";

    private static JSONObject handleOKHttpRequest(String type, JSONArray dataJson) {
        JSONObject paramsJson = new JSONObject();
        JSONObject inuptJson = new JSONObject();
        String fullDate = DateUtil.getFullDate();
        String curdate = fullDate.substring(0, 10).replace("-", "");
        String curtime = fullDate.substring(11, 19).replace(":", "");
        JSONObject headerJson = new JSONObject();
        headerJson.put("SYSID", "EC");
        headerJson.put("BUSINESS", type);
        headerJson.put("BUSNKEY", "YEJAJFHJDHKA");
        headerJson.put("TECHKEY", getUUIDStr());
        headerJson.put("DATE", curdate);
        headerJson.put("TIME", curtime);
        inuptJson.put("HEADER", headerJson);
        inuptJson.put("DATA", dataJson);
        paramsJson.put("INPUT", inuptJson.toJSONString());
        new BaseBean().writeLog("handleOKHttpRequest>>>>paramsJson="+paramsJson);
        return sendOKHttpRequest(paramsJson);
    }

    private static JSONObject sendOKHttpRequest(JSONObject params) {
        JSONObject resultJson = new JSONObject();
        String code = "fail";
        String message = "";
        String data = "";
        try {
            String authorization = " Basic " + Base64.encode((USERNAME + ":" + PASSWORD).getBytes());
            OkHttpClient client = new OkHttpClient().newBuilder()
                    .connectTimeout(60, TimeUnit.SECONDS)
                    .readTimeout(60, TimeUnit.SECONDS)
                    .writeTimeout(60, TimeUnit.SECONDS)
                    .build();
            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, params.toJSONString());
            Request request = new Request.Builder()
                    .url(URL)
                    .method("POST", body)
                    .addHeader("Authorization", authorization)
                    .addHeader("Content-Type", "application/json")
                    .build();
            Response response = client.newCall(request).execute();
            data = Util.null2String(response.body().string());
            if (!"".equals(data)) {
                resultJson.put("data", JSONObject.parseObject(data));
                code = "success";
            } else {
                message = "response result is null";
            }
        } catch (Exception e) {
            message = "response timeout";
            e.printStackTrace();
        }
        resultJson.put("code", code);
        resultJson.put("message", message);
        return resultJson;
    }

    private static String getUUIDStr() {
        String uuid = UUID.randomUUID().toString();
        return uuid.replace("-", "");
    }

%>



